package com.taru.wxhsh.api;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.taru.wxhsh.common.JsonResult;
import com.taru.wxhsh.constant.JResponseConstants;
import com.taru.wxhsh.controller.ProductController;
import com.taru.wxhsh.pojo.Product;
import com.taru.wxhsh.service.ProductService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *
 */
@RestController
@CrossOrigin
public class ProductApi {
    Logger logger  = LoggerFactory.getLogger(ProductApi.class);

    @Autowired
    ProductService productService;
    /**
     * 商品查询
     * @return
     */
    @GetMapping("/api/product/list")
    public Object list(@RequestParam(value="pageNum",defaultValue = "1",required = false) Integer pageNum,
                       @RequestParam(value = "pageSize" ,defaultValue = "8" ,required = false) Integer pageSize)
    {
        JsonResult<PageInfo<Product>> result=null;
        try{
            PageHelper.startPage(pageNum,pageSize);
            List<Product> products =productService.selectBasicInfoList();
            PageInfo<Product> pageInfo =new PageInfo<>(products,pageSize);

            result =new JsonResult<PageInfo<Product>>(JResponseConstants.STATUS_OK,"小程序查询商品列表成功",pageInfo);
        }catch (Exception ex){
            logger.error("小程序查询商品列表异常:{}",ex.getMessage());
            result =new JsonResult<PageInfo<Product>>(JResponseConstants.STATUS_ERROR,"小程序查询商品列表异常");
        }
        return  result;

    }


    /**
     * 商品查询根据ID
     * @return
     */

    @GetMapping("/api/product/find")
    public Object find(String  productId){
        JsonResult<Product>   result=null;
        try{

            Product product =productService.findProductById(productId);
            result =new JsonResult<>(JResponseConstants.STATUS_OK,"查询商品成功",product);
        }catch (Exception ex){
            ex.printStackTrace();
            logger.error("查询商品异常:{}",ex.getMessage());
            result =new JsonResult<>(JResponseConstants.STATUS_ERROR,"查询商品异常");
        }
        return  result;

    }


    /**
     *  根据类别商品查询
     * @return
     */
    @GetMapping("/api/product/selectByCatrgory")
    public Object select(@RequestParam(value="pageNum",defaultValue = "1",required = false) Integer pageNum,
                       @RequestParam(value = "pageSize" ,defaultValue = "20" ,required = false) Integer pageSize,
                         @RequestParam(value = "categoryId") String categoryId)

    {
        JsonResult<PageInfo<Product>> result=null;
        try{
            PageHelper.startPage(pageNum,pageSize);
            List<Product> products =productService.findProductsByCategory(categoryId);
            PageInfo<Product> pageInfo =new PageInfo<Product>(products,pageSize);
            result =new JsonResult<PageInfo<Product>>(JResponseConstants.STATUS_OK,"小程序查询商品列表成功",pageInfo);

        }catch (Exception ex){
            logger.error("小程序查询商品列表异常:{}",ex.getMessage());
            result =new JsonResult<>(JResponseConstants.STATUS_ERROR,"小程序查询商品列表异常");
        }
        return  result;

    }




    /**
     *  查询历史购买商品
     * @return
     */
    @GetMapping("/api/product/selectHistoryProducts")
    public Object selectHisProducts(@RequestParam(value="pageNum",defaultValue = "1",required = false) Integer pageNum,
                         @RequestParam(value = "pageSize" ,defaultValue = "20" ,required = false) Integer pageSize,
                         @RequestParam(value = "userId") String userId)

    {
        JsonResult<PageInfo<Product>> result=null;
        try{
            PageHelper.startPage(pageNum,pageSize);
            List<Product> products =productService.selectHistoryProducts(userId);
            PageInfo<Product> pageInfo =new PageInfo<Product>(products,pageSize);
            result =new JsonResult<PageInfo<Product>>(JResponseConstants.STATUS_OK,"小程序查询历史列表成功",pageInfo);

        }catch (Exception ex){
            logger.error("查询历史商品列表异常:{}",ex.getMessage());
            result =new JsonResult<PageInfo<Product>>(JResponseConstants.STATUS_ERROR,"小程序查询历史商品列表异常");
        }
        return  result;

    }

    /**
     * 修改商品的基本信息
     * @param product
     * @return
     */
    @PostMapping ("/api/product/update")
    public Object update(Product product){
        JsonResult<String>   result=null;
        try{

            productService.updateProduct(product);
            result =new JsonResult<>(JResponseConstants.STATUS_OK,"修改商品成功");
        }catch (Exception ex){
            ex.printStackTrace();
            logger.error("修改商品异常:{}",ex.getMessage());
            result =new JsonResult<>(JResponseConstants.STATUS_ERROR,"修改商品异常");
        }
        return  result;

    }

    @RequestMapping ("/api/product/updatephoto")
    public Object updatephoto(String productId,String photo){
        JsonResult<String>   result=null;
        try{
            productService.updatePhoto(productId,photo);
            result =new JsonResult<>(JResponseConstants.STATUS_OK,"修改头像成功");
        }catch (Exception ex){
            ex.printStackTrace();
            logger.error("修改头像异常:{}",ex.getMessage());
            result =new JsonResult<>(JResponseConstants.STATUS_ERROR,"修改头像异常");
        }
        return  result;

    }


    /**
     * 模糊搜索商品

     * @return
     */
    @GetMapping("/api/product/search")
    public Object search( @RequestParam(value = "name") String name)

    {
        JsonResult<List<Product>>   result=null;
        try{

            List<Product> products =productService.selectByLikeName(name);
            result =new JsonResult<List<Product>>(JResponseConstants.STATUS_OK,"搜索商品成功",products);
        }catch (Exception ex){
            ex.printStackTrace();
            logger.error("搜索商品异常:{}",ex.getMessage());
            result =new JsonResult<List<Product>>(JResponseConstants.STATUS_ERROR,"搜索商品异常");
        }
        return  result;

    }








}
